package com.web3.management.controller;

import com.web3.management.dto.ApiResponse;
import com.web3.management.dto.PageResponse;
import com.web3.management.dto.request.ConfigRequest;
import com.web3.management.dto.response.ConfigDetailResponse;
import com.web3.management.dto.response.ConfigHistoryResponse;
import com.web3.management.dto.response.ConfigListResponse;
import com.web3.management.service.ConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;

/**
 * 全局配置控制器
 */
@RestController
@RequestMapping("/api/configs")
@RequiredArgsConstructor
@Validated
public class ConfigController {

    private final ConfigService configService;

    @GetMapping
    public ApiResponse<PageResponse<ConfigListResponse>> listConfigs(
            @RequestParam(defaultValue = "1") @Min(1) Integer page,
            @RequestParam(defaultValue = "10") @Min(1) Integer size,
            @RequestParam(required = false) String name) {
        PageResponse<ConfigListResponse> response = configService.getConfigPage(page, size, name);
        return ApiResponse.success(response);
    }

    @GetMapping("/{id}")
    public ApiResponse<ConfigDetailResponse> getConfig(@PathVariable @Min(1) Integer id) {
        return ApiResponse.success(configService.getConfigDetail(id));
    }

    @PostMapping
    public ApiResponse<ConfigDetailResponse> createConfig(@Valid @RequestBody ConfigRequest request) {
        ConfigDetailResponse response = configService.createConfig(request);
        return ApiResponse.success("配置创建成功", response);
    }

    @PutMapping("/{id}")
    public ApiResponse<ConfigDetailResponse> updateConfig(@PathVariable @Min(1) Integer id,
                                                          @Valid @RequestBody ConfigRequest request) {
        ConfigDetailResponse response = configService.updateConfig(id, request);
        return ApiResponse.success("配置更新成功", response);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteConfig(@PathVariable @Min(1) Integer id) {
        configService.deleteConfig(id);
        return ApiResponse.success("配置删除成功", null);
    }

    @PostMapping("/{id}/activate")
    public ApiResponse<Void> activateConfig(@PathVariable @Min(1) Integer id) {
        configService.activateConfig(id);
        return ApiResponse.success("配置已激活", null);
    }

    @GetMapping("/{id}/history")
    public ApiResponse<PageResponse<ConfigHistoryResponse>> getHistory(@PathVariable @Min(1) Integer id,
                                                                       @RequestParam(defaultValue = "1") @Min(1) Integer page,
                                                                       @RequestParam(defaultValue = "10") @Min(1) Integer size) {
        PageResponse<ConfigHistoryResponse> response = configService.getConfigHistory(id, page, size);
        return ApiResponse.success(response);
    }
}
